package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.params.PageQueryParam;
import com.itheima.common.utils.QiniuUtils;
import com.itheima.common.vo.PageResult;
import com.itheima.common.vo.Result;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.mapper.SetMealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.SetMeal;
import com.itheima.service.SetMealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service(version = "1.0",interfaceClass = SetMealService.class)
@Transactional
public class SetMealServiceImpl implements SetMealService {
    @Resource
    private SetMealMapper setMealMapper;
    @Resource
    private CheckGroupMapper checkGroupMapper;
    @Resource
    private CheckItemMapper checkItemMapper;

    @Override
    public Result save(SetMeal setMeal) {

        this.setMealMapper.save(setMeal);
        Integer setMealId = setMeal.getId();

        List<Integer> checkGroupIds = setMeal.getCheckGroupIds();
        for (Integer checkGroupId : checkGroupIds) {
            setMealMapper.saveSetMealAndGroup(setMealId,checkGroupId);
        }
        return Result.success(null);
    }

    @Override
    public Result findPage(PageQueryParam pageQueryParam) {
        PageHelper.startPage(pageQueryParam.getCurrentPage(),pageQueryParam.getPageSize());

        Page<SetMeal> setMealPage = setMealMapper.findByCondition(pageQueryParam.getQueryString());

        PageResult pageResult = new PageResult();
        pageResult.setList(setMealPage.getResult());
        pageResult.setTotal(setMealPage.getTotal());
        return Result.success(pageResult);
    }

    @Override
    public Result findAll() {
        List<SetMeal> setMealList = this.setMealMapper.findAll();
        //数据库图片 只存了文件名
        List<SetMeal> collect = setMealList.stream().map(setMeal ->
                {
                    setMeal.setImg(QiniuUtils.prefixUrl + setMeal.getImg());
                    return setMeal;
                }
        ).collect(Collectors.toList());
        return Result.success(collect);
    }

    @Override
    public Result findById(Integer id) {
        //1. 查询套餐 需要根据套餐的id 查询对应的检查组列表
        //2. 查询检查组列表后，需要查询检查组对应的检查项列表
        SetMeal setMeal = this.setMealMapper.findById(id);
        setMeal.setImg(QiniuUtils.prefixUrl+setMeal.getImg());
//        List<CheckGroup> checkGroupList = this.checkGroupMapper.findGroupsByMealId(id);
//        setMeal.setCheckGroups(checkGroupList);
//
//        for (CheckGroup checkGroup : checkGroupList) {
//            Integer checkGroupId = checkGroup.getId();
//            List<CheckItem> checkItemList = this.checkItemMapper.findItemsByGroupId(checkGroupId);
//            checkGroup.setCheckItems(checkItemList);
//        }
        return Result.success(setMeal);
    }
    @Autowired
    private FreeMarkerConfigurer freemarkerConfig;
    @Value("${out_put_path}")
    private String mobilePath;
    @Override
    public Result gen() {
        try {
            //5.数据模型
            List<SetMeal> setMealList = this.setMealMapper.findAll();
            //数据库图片 只存了文件名
            List<SetMeal> collect = setMealList.stream().map(setMeal ->
                    {
                        setMeal.setImg(QiniuUtils.prefixUrl + setMeal.getImg());
                        return setMeal;
                    }
            ).collect(Collectors.toList());
            Map<String, Object> map = new HashMap<>();
            map.put("setMealList",collect);
            genTemplate("setmeal.ftl",map,"setmeal_mobile.html");

            //套餐详情生成
            for (SetMeal sm : setMealList) {
                SetMeal setMeal = this.setMealMapper.findById(sm.getId());
                setMeal.setImg(QiniuUtils.prefixUrl+setMeal.getImg());
                Map<String, Object> setMealMap = new HashMap<>();
                setMealMap.put("setmeal",setMeal);
                genTemplate("setmeal_detail.ftl",setMealMap,"setmeal_"+sm.getId()+".html");
            }

            return Result.success(null);
        }catch (Exception e){
            e.printStackTrace();
        }

        return Result.fail();

    }

    public void genTemplate(String templateName,Object dataModel,String target) throws Exception{
        //调用此方法，生成 套餐列表和套餐详情的静态页面
        Configuration configuration = freemarkerConfig.getConfiguration();
        //4.加载模板
        Template template = configuration.getTemplate(templateName);
        Writer writer = new FileWriter(new File(mobilePath+target));
        template.process(dataModel,writer);
        writer.close();
    }
}
